home *** CD-ROM | disk | FTP | other *** search
- From: ChrisHines@msn.com (Chris Hines)
- Subject: RE: INITIALIZATION of a MEMBER CLASS of a CLASS
- Date: 21 Apr 96 02:41:01 -0700
- References: <4l37o3$iug@risky.ecs.umass.edu>
- Message-ID: <00001a81+0000b279@msn.com>
- Path: news.msn.com!msn.com
- Newsgroups: comp.lang.c++
- Organization: The Microsoft Network (msn.com)
-
- Kiran Toutireddy writes:
-
- <snip>
-
- > class Engine{
- > nuts..bolts..pipes..rods...;
- > public:
- > Engine(){ make this an electric engine,....;}
- > Engine(int Gas){ make it a gas engine..}
- > Engine(float Deisel){ make it a deisel engine;}
- > };
- >
- >
- > class Car{
- > Engine car_engine;
- > public:
- > Car(){ how do i initialize the car_engine to be of type Gas here?;}
- > };
- >
- >I tried initializing the car_engine by saying Engine car_engine(int
- Gas), but it >gives a warning saying that ANSI c++ forbids such
- declarations.
- >
- >Can anybody suggest a better way of doing this?
- >thanks in advance
- >-kiran
-
- Try this, write the implementation of the constructor for Car as follows:
-
- Car::Car() : car_engine(1)
- {
- <snip>
- }
-
- The constructor for car_engine is called prior to the body of the
- constructor for Car. If you have Stroustrup, look up Member,
- constructor for class in the index.
-
- ------
- ChrisHines@msn.com
- The sooner you start coding, the longer it will take you to finish.
-